﻿var CreatedOKLodop7766=null;
var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
var oscript = document.createElement("script");
oscript.src ="http://localhost:8000/CLodopfuncs.js?priority=1";
head.insertBefore( oscript,head.firstChild );
//引用双端口(8000和18000）避免其中某个被占用：
oscript = document.createElement("script");
oscript.src ="http://localhost:18000/CLodopfuncs.js?priority=0";
head.insertBefore(oscript, head. firstChild);

var defaultPrinter = null;
var printerIP = null;

function getLodop(strTaskName) {
	var LODOP = getCLodopA();
	LODOP.PRINT_INIT(strTaskName);
	LODOP.SET_PRINT_STYLE("Bold", 1);
	LODOP.SET_SHOW_MODE("NP_NO_RESULT", 1);
	LODOP.SET_SHOW_MODE("HIDE_SBUTTIN_PREVIEW", 1);
	LODOP.SET_SHOW_MODE("HIDE_QBUTTIN_PREVIEW", 1);
	LODOP.SET_SHOW_MODE("PREVIEW_NO_MINIMIZE", 1);
	LODOP.SET_PRINT_MODE("AUTO_CLOSE_PREWINDOW", 1);
	LODOP.SET_PREVIEW_WINDOW(2, 0, 1, 760, 540, strTaskName + ".打印");

	//获取本地打印机列表
	var printCount = LODOP.GET_PRINTER_COUNT();
	var localPrinterList = {};
	
	var index = -1;
	for(var i = 0;i < printCount;i ++){
		var curLocalPrinter = LODOP.GET_PRINTER_NAME(i + ":DriverName");
		localPrinterList[curLocalPrinter] = curLocalPrinter;
		if(defaultPrinter == curLocalPrinter) {
			index = i;
			break;
		}
	}
	
	LODOP.SET_PRINTER_INDEX(index);
	return LODOP;
}
//获取CLODOP
function getCLodopA(){
	var LODOP;
	try {
		try {
			LODOP = getCLodop();
		} catch (err) {
		}
		if (!LODOP && document.readyState !== "complete") {
			alert("C-Lodop没准备好，请稍后再试！");
			return;
		}
		if (typeof(LODOP) == 'undefined') {
			installLodop("CLodop_Setup_for_Win32NT.exe");
		}
		return LODOP;
	} catch (err) {
		alert("getLodop出错:" + err);
	}
}

//下载安装CLodop
function installLodop(name) {
	var post = window.location.protocol + "//" + window.location.host + "/mes/";
	var url = post + "web/com.basic/js/lodop/" + name;
	window.open(url);
}
function printData(json, codeType, size) {
	var data = {};
	for (var key in json) {
		data[key] = json[key];
	}
	data["code"] = codeType;
	$.post("org.openbravo.client.kernel?_action=com.basis.barcode.DefaultPrinterHandler", data, 
		function (res) {
			if (res.success) {
				defaultPrinter = res.printerName;
				printerIP = res.printerIP;
				var columns = res.columns;
				var data = res.data;
				var listType = res.listType;
				var LODOP = getLodop();
				
				LODOP.ADD_PRINT_HTML(0, 0, 0, 0, 
						'<style>table{border: 2px #000 solid;}table th,td{border: 1px #000 solid;padding: 3px;}</style>');
				for (var i = 0;i < data.length;i ++) {
					var newData = {};
					var tempd = data[i];
					var tempdv = [];
					for(var ii in tempd){
						if(!newData[ii]){
							newData[ii] = tempd[ii];
						} else {
							newData[ii] += "," + tempd[ii];
						}
					}
					if (i > 0) {
						LODOP.NEWPAGE();
					}
					
					if (data[i]._Data_List && listType == 'LIST') {
						printPageList(columns, data[i]._Data_List, res.tablePostion);
					}
					eval(parseHtml(res.html, newData));
				}
				//设置打印份数
				if (size > 1) {
					LODOP.SET_PRINT_COPIES(size);
				}
				
				setTimeout(function() {
					LODOP.PREVIEW();
				}, 500);
			} else if (res.list) {
				//打印机选择
				
			} else {
				isc.showMessage(res.msg);
			}
		}, "json");
}
function parseHtml(html, data) {
	var reg = /\{\[(.*?)\]\}/gi;
	var codes = html.match(reg);
	var code = html;
	if (codes) {
		var value = "";
		var match = '';
		for (var i = 0;i < codes.length;i ++) {
			match = codes[i].replace("{[", "").replace("]}", "");
			value = data[match.replace("data.", "").toUpperCase()];
			if (!value) {
				value = "";
			}
			code = code.replace(codes[i], value);
		}
	}
	return code;
}
function printPageList(columns, list, postion) {
	if (postion) {
		postion = postion.split(",");
	} else {
		postion = [0, 0, 400, 200];
	}
	var html = ['<style>table{border: 2px #000 solid;}table th,td{border: 1px #000 solid;padding: 3px;}</style>', 
				'<table style="border-collapse: collapse;width: ', (postion ? postion[2] : 400), 
				'px;" cellpadding="3"><tr>'];
	for (var i = 0;i < columns.length;i ++) {
		html.push('<th style="width: ' + (columns[i].WIDTH ? columns[i].WIDTH : 40) + 'px;background-color: #eee;' + columns[i].TITLE_STYLE + '">' + columns[i].TITLE + '</th>');
	}
	html.push('</tr>');
	for (var j = 0;j < list.length;j ++) {
		html.push('<tr>');
		for (var i = 0;i < columns.length;i ++) {
			html.push('<td style="' + columns[i].DATA_STYLE + '">' + isNotNull(list[j][columns[i].COLUMN_NAME.toUpperCase()]) + '</td>');
		}
		html.push('</tr>');
	}
	html.push('</table>');
	LODOP.ADD_PRINT_HTML(postion[0], postion[1], postion[2], postion[3], html.join(""));
}
function isNotNull(value) {
	return value ? value : "";
}
//阿拉伯数字转中文大写
function numberToZWDX(num){
	var DX=["零", "一", "二", "三", "四", "五", "六", "七", "八", "九","十"];
	if(num>=0 && num<=10){
		return DX[num];
	}
	return num;
}